package com.ruoyi.store.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.store.domain.*;
import com.ruoyi.store.mapper.*;
import com.ruoyi.store.service.IStuExamDetailService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StuExamDetailImpl implements IStuExamDetailService {
    @Resource
    private StuExamDetailMapper  stuExamDetailMapper;
    @Resource
    private StuPaperMapper stuPaperMapper;
    @Resource
    private StuUserAnswerMapper stuUserAnswerMapper;
    @Resource
    private StuQuestionMapper stuQuestionMapper;
    @Resource
    private StuQuesAnwMapper stuQuesAnwMapper;
    @Resource
    private RedisCache redisCache;


    @Override
    public List<StuExamDetail> selectStuExamDetailList(StuExamDetail stuExamDetail) {
        return stuExamDetailMapper.selectStuExamDetailList(stuExamDetail);
    }

    //根据用户id查询历史试卷
    @Override
    public List<StuUserAnswer> selectStuHisPaperByUserId(Long userId, Long paperId) {
        List<StuUserAnswer> stuUserAnswers = stuUserAnswerMapper.selectStuHisPaperByUserId(userId, paperId);
        for (StuUserAnswer stuUserAnswer : stuUserAnswers){
            Long quesId = stuUserAnswer.getQuesId();
            StuQuestion stuQuestion = stuQuestionMapper.selectQuesById(quesId);
            String quesName = stuQuestion.getQuesName();
            stuUserAnswer.setQuesName(quesName);
        }

        return stuUserAnswers;
    }

    @Override
    //添加分数
    public int addMark(StuHisExam stuHisExam) {
        List<StuUserAnswer> stuUserAnswers = stuHisExam.getOptionList();
        long score = 0;
        for (StuUserAnswer stuUserAnswer:stuUserAnswers){
            //添加主观题题分数
            if (stuUserAnswer.getQuesType().equals("简答题")){
                Long answerScore = stuUserAnswer.getAnswerScore();
                score += answerScore;
//                stuUserAnswer.setAnswerScore(answerScore);
//                stuUserAnswerMapper.updateStuExamHistory(stuUserAnswer);
            }

        }
        //将客观题以及总分插入细节表中
        Long userId = stuHisExam.getUserId();
        Long paperId = stuHisExam.getPaperId();
        StuExamDetail stuExamDetail = stuExamDetailMapper.selectStuExamDetailByUserIdPaperId(userId, paperId);
        //拿到客观题分数
        long objScore = stuExamDetail.getObjScore();
        long totalScore = objScore + score;

        stuExamDetail.setAnswerScore(score);
        stuExamDetail.setTotalScore(totalScore);
        //添加主观题分数和总分
        stuExamDetailMapper.updateScore(stuExamDetail);
        return 1;
    }

    @Override
    public List<StuUserAnswer> selectStuHisPaper(Long userId, Long paperId) {
        //将学生历史答卷缓存到Redis中去
        String key = "stuExamHis:" + userId + ":"+ paperId;
        List<StuUserAnswer> cacheList = redisCache.getCacheList(key);
        if (!cacheList.isEmpty()){
            return cacheList;
        }

        List<StuUserAnswer> stuUserAnswers = stuUserAnswerMapper.selectStuHisPaper(userId, paperId);
        for (StuUserAnswer stuUserAnswer : stuUserAnswers){
            Long quesId = stuUserAnswer.getQuesId();
            StuQuestion stuQuestion = stuQuestionMapper.selectQuesById(quesId);
            StuQuesAnwser stuQuesAnwser = stuQuesAnwMapper.selectAnwSerById(quesId);
            if (stuQuestion.getQuesType().equals("单选题") || stuQuestion.getQuesType().equals("多选题")){
                stuUserAnswer.setSelContA(stuQuesAnwser.getSelContA());
                stuUserAnswer.setSelContB(stuQuesAnwser.getSelContB());
                stuUserAnswer.setSelContC(stuQuesAnwser.getSelContC());
                stuUserAnswer.setSelContD(stuQuesAnwser.getSelContD());
            }
            String quesName = stuQuestion.getQuesName();
            stuUserAnswer.setQuesName(quesName);
        }

        redisCache.setCacheList(key,stuUserAnswers);
        return stuUserAnswers;
    }

    @Override
    public StuExamDetail selectStuExamDetailByUserIdPaperId(Long userId, Long paperId) {
        return stuExamDetailMapper.selectStuExamDetailByUserIdPaperId(userId,paperId);
    }

    @Override
    public List<StuExamDetail> selectStuExamDetailListByUserId(Long userId) {
        return stuExamDetailMapper.selectStuExamByUserId(userId);
    }


}
